// pages/associationDetail/associationDetail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: app.data.imgUrl,
    id: '',
    detail: {},
    newsList: [],
    params: {
      page: 1,
      numPerPage: 10,
      associationId: '',
    },
    isAdminLogin: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id,
      'params.associationId': options.id
    })
    // console.log('29')
    // console.log(this.data.id)
    this.getDetail(options.id) //获取进入社团的信息
    this.getNewsList(this.data.params)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //同步一级评论数据
    if (app.globalData.oneSynchroData) {
      this.updataInfoList(app.globalData.oneSynchroData)
    }
  },
  //遍历infoList，修改点赞数，状态
  updataInfoList(oneSynchroData) {
    for (var item in this.data.newsList) {
      if (this.data.newsList[item].id == app.globalData.oneSynchroData.id) {
        console.log(this.data.newsList[item])
        this.data.newsList[item].upNum += app.globalData.oneSynchroData.dainzan
        if (this.data.newsList[item].upStatus == 1 && app.globalData.oneSynchroData.dainzan < 0) {
          this.data.newsList[item].upStatus = 0
        } else if (this.data.newsList[item].upStatus == 0 && app.globalData.oneSynchroData.dainzan > 0) {
          this.data.newsList[item].upStatus = 1
        }
        this.data.newsList[item].commentNum += app.globalData.oneSynchroData.oneCommentNum
      }
    }
    this.setData({
      newsList: this.data.newsList
    })
    app.globalData.oneSynchroData = null
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refrish()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.params.page += 1
    this.getNewsList(this.data.params)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '我新发现了一个有趣社团，我在这里等你哟',
      imgUrl: 'https://aa-1257564155.cos.ap-chengdu.myqcloud.com/tmp_955d50b786dc9776a422b990a07f26cc.jpg',
      path: '/pages/login/index?url=/pages/associationDetails/associationDetails&id=' + this.data.id
    }
  },
  //跳转页面
  goto(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  //加载大图
  showImg(e) {
    wx.previewImage({
      urls: [e.currentTarget.dataset.src],
    })
  },
  //加载大图
  previewImage(e) {
    let index = e.currentTarget.dataset.index
    let urls = []
    for (let i = 1; i < 10; i++) {
      let url = this.data.newsList[index]['pic' + i]
      if (url) {
        urls.push(url)
      }
    }
    wx.previewImage({
      urls: urls,
    })
  },
  //获取进入社团的信息
  getDetail(id) {
    wx.teamApi.association.detail(id).then(res => {
      console.log('114')
      console.log(res)
      //获取社团信息的同时判断管理员是否进入了自己社团主页
      let admin = wx.getStorageSync("adminUserInfo")
      // isAdminLogin
      if (admin != "") {
        this.setData({
          isAdminLogin: true
        })
      }
      let tempName = ''
      if (res.data.result.name.length > 14) {
        tempName = res.data.result.name.substring(0, 14) + '...'
        res.data.result.name = tempName
      }
      this.setData({
        detail: res.data.result
      })
    })

  },
  loadMore() {
    let nextPage = this.data.params.page + 1
    this.setData({
      'params.page': nextPage
    })
    this.getNewsList(this.data.params)
  },
  //获取新闻列表
  getNewsList(params, bool) {
    wx.teamApi.association.newslist(params).then(res => {
      console.log('167')
      console.log(res)
      if (bool) {
        this.setData({
          newsList: res.data.result
        })
      } else {
        this.setData({
          newsList: this.data.newsList.concat(res.data.result)
        })
      }
    })
  },
  //关注申请、退出等等
  followClick() {
    switch (this.data.detail.isFollow) {
      case 0:
        this.follow()
        break;
      case 1:
        wx.showModal({
          title: '提示',
          content: '已经在申请关注中了，请耐心等待',
        })
        break;
      case 2:
        this.unfollow()
        break;
    }
    // this.data.detail.isFollow ? this.unfollow() : this.follow()
  },
  //加入事件
  joinClick() {
    switch (this.data.detail.isJoin) {
      case 0:
        this.join()
        break;
      case 1:
        // wx.showModal({
        //   title: '提示',
        //   content: '已经在申请加入中了，请耐心等待',
        // })

        break;
      case 2:
        this.quit()
        break;
    }
    // this.data.detail.isJoin ? this.quit() : this.join()
  },
  //
  follow() {
    wx.teamApi.association.follow(this.data.id).then(res => {
      console.log('182')
      console.log(res)
      if (res.data.errcode === 0) {
        if (res.data.result === 2) {
          wx.showToast({
            title: '关注成功！'
          })
        } else if (res.data.result === 1) {
          wx.showToast({
            icon: 'loading',
            title: '申请中！'
          })
        }
        this.refrish()
      } else {
        // wx.showToast({
        //   icon: 'none',
        //   title: res.data.err
        // })
      }
    })
  },
  //申请加入事件
  join() {
    console.log(206)
    console.log(this.data.id)
    wx.teamApi.association.join(this.data.id).then(res => {
      if (res.data.errcode === 0) {
        if (res.data.result === 2) {
          wx.showToast({
            title: '加入成功！'
          })
          // this.data.detail.isJoin = true
        } else if (res.data.result === 1) {
          wx.showToast({
            icon: 'loading',
            title: '申请中！'
          })
        } else if (res.data.result === 0) {
          wx.showToast({
            icon: 'none',
            title: '申请已被拒绝！'
          })
        }
        this.refrish()
      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.err
        })
      }
    })
  },
  //退出社团
  quit() {
    wx.teamApi.association.quit(this.data.id).then(res => {
      if (res.data.errcode === 0) {
        wx.showToast({
          title: '已退出该社团'
        })
        this.refrish()
      }
    })
  },
  //取消关注
  unfollow() {
    wx.teamApi.association.unfollow(this.data.id).then(res => {
      if (res.data.errcode === 0) {
        wx.showToast({
          title: '取消关注成功!'
        })
      }
      this.refrish()
    })
  },
  activityTap(e) {
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `../newsDetail/index?id=${item.id}`,
    })
  },
  newsTap(e) {
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `../newsDetail/index?id=${item.id}`,
    })
  },
  refrish() {
    this.data.params.page = 1
    this.getDetail(this.data.id)
    this.getNewsList(this.data.params, true)
  },


  //点赞事件
  zanClick(e) {
    let item = e.currentTarget.dataset.item
    let index = e.currentTarget.dataset.index
    console.log(item)
    if (item.upStatus) {
      this.cancelUpNews(item.id, index)
    } else {
      this.upNews(item.id, index)
    }
  },
  //加上赞
  upNews(id, index) {
    wx.teamApi.news.upNews(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.newsList[index].upStatus = 1
        this.data.newsList[index].upNum += 1
        //console.log(this.data.infoList[index])
        this.setData({
          newsList: this.data.newsList
        })
      }

    })
  },
  //取消赞
  cancelUpNews(id, index) {
    wx.teamApi.news.cancelUpNews(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.newsList[index].upStatus = 0
        this.data.newsList[index].upNum -= 1
        //console.log(this.data.infoList[index])
        this.setData({
          newsList: this.data.newsList
        })
      }

    })
  },

})